<div class="content">
	<div class="tab_bar">
		<ul>
			<li class="tab_index" onclick="setTabContent(this);">分类管理</li>
		</ul>
	</div>
	<div class="tab_content">
		<div class="tab_item tab_show">
            <div style="margin-bottom: 10px;">
                <button class="" action="category/add.do" onclick="refreshContentByElement(this);">添加分类</button>
                <button class="" action="category/addBatch.do" onclick="refreshContentByElement(this);">批量添加分类</button>
            </div>
            <div style="width: 30%; display: inline-block; vertical-align: top;">


                <ul id="treeDemo" class="ztree"></ul>

            </div>
            <div style="width: 60%; display: inline-block; vertical-align: top;">
            <form id="myForm" action="category/update.do" method="post" onsubmit="return false">
                <input type="hidden" id="categoryId" name="id" value="0" />
                <table class="table_for_add">
                    <tbody>
                    <tr class="rowBgColor">
                        <th width="140">分类ID：</th>
                        <td>
                            <span id="categoryId2"></span>
                        </td>
                        <td><span></span></td>
                    </tr>
                    <tr class="rowBgColor">
                        <th width="140">上级分类：</th>
                        <td>
                            <select id="categoryPid" class="select_for_add" name="pid">
                                <option value="0"> -------- 根级 -------- </option>
                                <@categoryTree id = 0 node = -1/>
                            </select>
                        </td>
                        <td></td>
                    </tr>

                    <tr>
                        <th width="140">名称：</th>
                        <td><input class="input_for_add" id="categoryName" name="name" type="text" value="" /></td>
                        <td><span  ></span></td>
                    </tr>

                    <tr class="rowBgColor">
                        <th width="140">排序：</th>
                        <td><input class="input_for_add" id="categorySort" name="sort" type="number" value="0"  /></td>
                        <td><span  ></span></td>
                    </tr>
                    <tr  >
                        <th>描述信息：</th>
                        <td>
                            <textarea class="textarea_for_add" id="categoryDesc" name="description"></textarea>
                        </td>
                        <td>
                            <span></span>
                        </td>
                    </tr>
                    </tbody>
                </table>

                <button class="" onclick="mySubmitForm(this);" return="category/list.do">保 存</button>

                <!--<button class="" action="category/add.do" onclick="refreshContentByElement(this);">保存</button>-->
                <button class="" id="deleteBtn" action="category/delete.do?rid=-1" return="category/list.do" onclick="deleteById(this);">删除</button>




            </form>
            </div>

		</div>
	</div>
</div>



<script type="text/javascript">
    var setting = {
        check: {
            enable: false,
            chkboxType :  { "Y" : "s", "N" : "ps" }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick:function(event, treeId, treeNode) {
                console.log(treeNode.tId + ", " + treeNode.name);
                console.log(treeNode.id)
                $.ajax({
                    url:'category/get.do',
                    type:'get',
                    data:{id: treeNode.id},
                    dataType:'json',
                    success:function(res){
                        console.log(res);
                        $("#categoryName").val(res.name);
                        $("#categorySort").val(res.sort);
                        $("#categoryDesc").val(res.description);
                        $("#categoryId").val(res.id);
                        $("#categoryId2").text(res.id);
                        $("#categoryPid").val(res.pid);
                        $("#categoryType").val(res.type);
                        $("#deleteBtn").attr("action","category/delete.do?id="+res.id);

                    }
                });



            },
            onCheck: function(event,treeId, treeNode){
                var zTree = $.fn.zTree.getZTreeObj("treeDemo"); //获取树
                var nodes = zTree.getNodes(); //获取每一个节点
                var nodeObjects = zTree.transformToArray(nodes);
                var selectData = [];
                for(var i=0; i <nodeObjects.length; i++){ //转换成数组
                    var node = nodeObjects[i];
                    if(node.checked){
                        selectData.push(node.id);
                    }
                }
                $("#channelIds").val(selectData.join(','));
            }
        }
    };
    var categoryList = "${channelsStr!}".split(",");
    var zNodes =[];
    $.ajax({
        url:'category/all.do',
        type:'get',
        dataType:'json',
        success:function(res){
            $.each(res,function(index,o){
                var node = {id:o.id,pId:o.pid,name:o.name};
                for(var i=0; i <categoryList.length; i++) { //转换成数组
                    var nodeId = categoryList[i];
                    if (node.id == nodeId) {
                        node.checked = true;
                    }
                }
                zNodes.push(node);
            });
            var tree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            tree.expandAll(true);
        }
    });
    function mySubmitForm(obj){
        submitActionForm(obj);
    }
</script>

<#macro categoryTree id node>
    <#local temp = node + 1 />
    <#list data as comment>
        <#if id = comment.pid>
            <option value="${comment.id?c}"><#list 0..temp as x> &nbsp; &nbsp; </#list>├ ${comment.name!}</option>
            <@categoryTree id = comment.id node=temp/>
        </#if>
    </#list>
</#macro>
